
local gaiju = fk.CreateSkill{
  name = "xiaobai__gaiju",
}

Fk:loadTranslationTable{

  ["xiaobai__gaiju"] = "改矩",
  [":xiaobai__gaiju"] = "每轮开始时，若你没有“矩”，你可以摸三张牌并将两张手牌置于武将牌上，称为“矩”；你使用点数不为“矩”之间的牌时弃置一张牌，"..
  "其他角色弃置点数不为“矩”之间的牌后你摸一张牌。",
  ["xiaobai__jiyan_rule"] = "矩",
  ["#xiaobai__gaiju-put"] = "改矩：请将两张手牌置为“矩”",

  ["$xiaobai__gaiju1"] = "碌碌庸人尸位禄，栋梁之才岂淹留！",
  ["$xiaobai__gaiju2"] = "倘若怀才，何居冗职？",
}

local function GaijuBetween(player, number)
  local numbers = table.map(player:getPile("xiaobai__jiyan_rule"), function (id)
    return Fk:getCardById(id).number
  end)
  return (number > numbers[1] and number < numbers[2]) or (number < numbers[1] and number > numbers[2])
end
gaiju:addEffect(fk.RoundStart,{
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and #player:getPile("xiaobai__jiyan_rule") == 0

  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:drawCards(3, gaiju.name)
    if player.dead or player:getHandcardNum() < 2 then return end
    local cards = room:askToCards(player, {
      min_num = 2,
      max_num = 2,
      include_equip = false,
      skill_name = gaiju.name,
      cancelable = false, 
      prompt = "#xiaobai__gaiju-put"})
    if Fk:getCardById(cards[1]).number > Fk:getCardById(cards[2]).number then
      cards[1], cards[2] = cards[2], cards[1]
    end
    player:addToPile("xiaobai__jiyan_rule", cards, true, gaiju.name, player)
  end,
})
gaiju:addEffect(fk.CardUsing, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and #player:getPile("xiaobai__jiyan_rule") == 2 then
      return target == player and not player:isNude() and #Card:getIdList(data.card) == 1 and
        not GaijuBetween(player, data.card.number)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:askToDiscard(player, {
      min_num = 1, 
      max_num = 1,
      include_equip = true,
      skill_name = gaiju.name,
      cancelable = false})
  end,
})

gaiju:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if #player:getPile("xiaobai__jiyan_rule") == 2 then
        for _, move in ipairs(data) do
          if move.from and move.from ~= player and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if not GaijuBetween(player, Fk:getCardById(info.cardId).number) then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:drawCards(1, gaiju.name)
  end,
})

return gaiju